package com.shz.controller;

import cn.hutool.json.JSONUtil;
import com.shz.dto.UserDTO;

import com.shz.pojo.ChatMsg;
import com.shz.pojo.User;
import com.shz.service.UserService;
import com.shz.utils.UserUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    UserService userService;

    @GetMapping("/all")
    public List<List<UserDTO>> getAllUsers() {
        Integer id = UserUtil.getCurrentUser().getId();
        List<List<UserDTO>> ans = new ArrayList<>();
        List<UserDTO> res = new ArrayList<>();
        List<String> list = stringRedisTemplate.opsForList().range("friendList:" + id, 0, -1);
        for (String s : list) {
            UserDTO u = JSONUtil.toBean(s, UserDTO.class);
            res.add(u);
        }
        ans.add(res);
        List<User> list2 = userService.query().list();
        ans.add(UserUtil.toUserDTOs(list2));
        return ans;
    }

    @DeleteMapping("/delete")
    public void removeFriendUserName(String username,String idd) {
        Integer id = UserUtil.getCurrentUser().getId();
        User u = userService.query().eq("username", username).one();
        stringRedisTemplate.opsForList().remove("friendList:" + id, Long.MAX_VALUE, JSONUtil.toJsonStr(UserUtil.toUserDTO(u)));
        stringRedisTemplate.opsForList().remove("idList:" + id,  Long.MAX_VALUE,idd);

    }



    @PostMapping("/add")
    public void addFriendUser(String username, String idd) {
        Integer id = UserUtil.getCurrentUser().getId();
        if (stringRedisTemplate.hasKey("idList:" + id)) {
            List<String> list = stringRedisTemplate.opsForList().range("idList:" + id, 0, -1);
            if (list.contains(idd)) {
                return;
            } else {
                User u = userService.query().eq("username", username).one();
                stringRedisTemplate.opsForList().leftPush("friendList:" + id, JSONUtil.toJsonStr(UserUtil.toUserDTO(u)));
                stringRedisTemplate.opsForList().rightPush("idList:" + id, idd);
            }
        } else {
            User u = userService.query().eq("username", username).one();
            stringRedisTemplate.opsForList().leftPush("friendList:" + id, JSONUtil.toJsonStr(UserUtil.toUserDTO(u)));
            stringRedisTemplate.opsForList().rightPush("idList:" + id, idd);
        }

    }
    @GetMapping("/getAllSessions")
    public  List<List<ChatMsg>> getAllChatSessions(){
        List<List<ChatMsg>> ans=new ArrayList<>();
        List<String> list2 = stringRedisTemplate.opsForList().range("sessionBackList:"+ UserUtil.getCurrentUser().getUsername(), 0, -1);
        List<ChatMsg> res2 = new ArrayList<>();
        for (String s : list2) {
            ChatMsg c = JSONUtil.toBean(s, ChatMsg.class);
            res2.add(c);
        }
        if(!stringRedisTemplate.hasKey("sessionList:"+ UserUtil.getCurrentUser().getUsername())){
            ans.add(Collections.emptyList());
            ans.add(res2);
            return  ans;
        }
        List<String> list = stringRedisTemplate.opsForList().range("sessionList:"+ UserUtil.getCurrentUser().getUsername(), 0, -1);
        List<ChatMsg> res = new ArrayList<>();
        for (String s : list) {
            ChatMsg c = JSONUtil.toBean(s, ChatMsg.class);
            res.add(c);
        }

        ans.add(res);
        ans.add(res2);
        stringRedisTemplate.delete("sessionList:"+ UserUtil.getCurrentUser().getUsername());
        return ans;
    }



}
